<?php
 
 class Post_Model_Index extends Zend_Db_Table
{
        protected $_name="post";
        protected $_primary = 'id';
	   
    public function insertPost($arrParam = null, $option = null)
	{
	  		$row = $this->fetchNew();		  	
			$row->title       =$arrParam['title'];
			$row->content       =$arrParam['content'];
			$row->userid           =$arrParam['userid'];
			$row->user_wall_id           =$arrParam['user_wall_id'];
			$row->status           =$arrParam['status'];
			$row->createdate       = $arrParam['createdate'];
			$row->categoryid       = $arrParam['categoryid'];
			$row->type       = $arrParam['type'];
			$row->link       = $arrParam['link'];
			$row->chanelid       = $arrParam['chanelid'];
			
			$id = $row->save();
			
			if(($arrParam['chanelid'] !="" || $arrParam['user_wall_id'] !="") && $arrParam['type'] != "rss"  && $arrParam['status'] == "publish"){
				

				// Insert data followed chanel
				
				if($arrParam['chanelid'] !=""){
					$chanel_follow = new Chanel_Model_FollowChanel();				
					$list_chanel_followed = $chanel_follow->listChanelByFollowed($arrParam['chanelid']);

					foreach( $list_chanel_followed as $key => $item){
						
						$feed = new Feed_Model_Index();
						$arr = array(
						"userfeedid"=>$item["follower"],						
						"postid"=>$id,
						"createdate"=>$arrParam['createdate'],
						"status"=>"new",
						"time_send_mail"=>"",
						"number_sendmail"=>1,																
						);
						
						$feed->insertFeed($arr);
						
						
					}
					
				}
				
				// Insert data followed user
				
				if($arrParam['user_wall_id'] !=""){
					$user_follow = new Registration_Model_Follow();				
					$list_chanel_followed = $user_follow->listUserByFollowed($arrParam['user_wall_id']);

					foreach( $list_chanel_followed as $key => $item){
						
						$feed = new Feed_Model_Index();
						$arr = array(
						"userfeedid"=>$item["follower"],						
						"postid"=>$id,
						"createdate"=>$arrParam['createdate'],
						"status"=>"new",
						"time_send_mail"=>"",
						"number_sendmail"=>1,											
						);
						
						$feed->insertFeed($arr);
						
					}
					
				}
				
				
			}
			
			$feed = new Feed_Model_Index();
			$arr = array(
				"userfeedid"=>$arrParam['userid'],						
				"postid"=>$id,
				"createdate"=>$arrParam['createdate'],
				"status"=>"new",
				"time_send_mail"=>"",
				"number_sendmail"=>1,																
			);						
			$feed->insertFeed($arr);
			
		  	return $id;
	}
	
	public function updatePost($arrParam = null, $option = null)
	{
		$db = Zend_Registry::get('connectDB');	
		$where = array(
    		'id = ?' => $arrParam['id'],
			'userid = ?' => $arrParam['userid'],		
		);	
		$data  = array(
			"title"=>$arrParam['title'],
			"content"=>$arrParam['content'],
			"status"=>$arrParam['status'],
			"categoryid" => $arrParam['categoryid'],
			"createdate"=>$arrParam['createdate'],			
		);
		$db->update("post", $data, $where);
	}
	
	public function countPost($arrParam = null, $option = null){

  		$db = Zend_Registry::get('connectDB');
  		$count = $db->fetchOne("SELECT COUNT(*) AS count FROM post where (userid = '".$arrParam['userid']."' or user_wall_id = '".$arrParam['userid']."') and status = '".$arrParam['status']."'" );
  		
  		return $count;
  		
  	}
  	
	public function countPostChanel($arrParam = null, $option = null){

  		$db = Zend_Registry::get('connectDB');
  		$count = $db->fetchOne("SELECT COUNT(*) AS count FROM post where chanelid = '".$arrParam['chanelid']."' and status = '".$arrParam['status']."'" );
  		
  		return $count;
  		
  	}
  	
	public function countPostHome($arrParam = null, $option = null){

  		$db = Zend_Registry::get('connectDB');
  		
  		if($arrParam['categoryid'] != ""){
  			
  			  			
  			$count = $db->fetchOne("SELECT COUNT(*) AS count FROM post where categoryid = '".$arrParam['categoryid']."' and  status = '".$arrParam['status']."'" );
  			
  		}else{
  			
  			$count = $db->fetchOne("SELECT COUNT(*) AS count FROM post where  status = '".$arrParam['status']."'" );
  			
  		}
  		
  		return $count;
  		
  	}
  	
	public function countPostFeed($arrParam = null, $option = null){

  		$db = Zend_Registry::get('connectDB');
  			  			
  		$count = $db->fetchOne("select count(*) as count  from post inner join feeduser on post.id =feeduser.postid  where feeduser.userfeedid = '".$arrParam['userfeedid']."'");
  		
  		return $count;
  		
  	}
  	
	
  	public function getItem($idpost){
  			
  		
  		$db = Zend_Registry::get('connectDB');				
  		$sql = "select * from post where id = '".$idpost."' and status = 'publish'";
  		$result = $db->fetchRow($sql);
		
  		return $result;
  		
  	}
  	
  	public function deletePost($idpost){
  		
  		$db = Zend_Registry::get('connectDB');	
		$where = array(
    		'id = ?' => $idpost,					
		);	
		$data  = array(
			"status"=>'deleted',
						
		);
		$db->update("post", $data, $where);
  		
  	}
  	
	public function listItemId($arrParam = null, $idpost){
		
		$db = Zend_Registry::get('connectDB');
		$pagesize = $arrParam['pagesize'];	
  		$offset = ($arrParam['page'] - 1) * $pagesize;		
  		$sql = "select * from post where (userid = '".$arrParam['userid']."' or user_wall_id = '".$arrParam['userid']."') and id <> '".$idpost."' and status = '".$arrParam['status']."' order by id DESC limit ".$offset.",".$pagesize." ";
  		$result = $db->fetchAll($sql);
		
  		return $result;
  	}
	
	public function listItem($arrParam = null, $option = null){
		
		$db = Zend_Registry::get('connectDB');
		$pagesize = $arrParam['pagesize'];	
  		$offset = ($arrParam['page'] - 1) * $pagesize;		
  		$sql = "select * from post where (userid = '".$arrParam['userid']."' or user_wall_id = '".$arrParam['userid']."') and status = '".$arrParam['status']."' order by id DESC limit ".$offset.",".$pagesize." ";
  		$result = $db->fetchAll($sql);
		
  		return $result;
  	}
  	
	public function listItemChanel($arrParam = null, $option = null){
		
		$db = Zend_Registry::get('connectDB');
		$pagesize = $arrParam['pagesize'];	
  		$offset = ($arrParam['page'] - 1) * $pagesize;		
  		$sql = "select * from post where chanelid = '".$arrParam['chanelid']."' and status = '".$arrParam['status']."' order by id DESC limit ".$offset.",".$pagesize." ";
  		$result = $db->fetchAll($sql);
		
  		return $result;
  	}
  	
	public function listItemChanelUser($arrParam = null, $option = null){
		
		$db = Zend_Registry::get('connectDB');
		$pagesize = $arrParam['pagesize'];	
  		$offset = ($arrParam['page'] - 1) * $pagesize;		
  		$sql = "select * from post inner join user on post.userid = user.id where chanelid = '".$arrParam['chanelid']."' and status = '".$arrParam['status']."' order by post.id DESC limit ".$offset.",".$pagesize." ";
  		$result = $db->fetchAll($sql);
		
  		return $result;
  	}
  	
	public function listItemAll($arrParam = null, $option = null){
		
		$db = Zend_Registry::get('connectDB');
		$pagesize = $arrParam['pagesize'];	
  		$offset = ($arrParam['page'] - 1) * $pagesize;
  		
  		$length = $arrParam['total'] - $offset;
  		
  		if ($length < 0 ){
  			$offset = ($arrParam['page'] - 1) * $length;
  		}
  		
  		
		if($arrParam['categoryid'] != ""){
			$sql = "select * from post where categoryid = '".$arrParam['categoryid']."' and status = '".$arrParam['status']."' order by chanelid DESC limit ".$offset.",".$pagesize." ";
		}else{

			$sql = "select * from post where status = '".$arrParam['status']."' order by chanelid DESC limit ".$offset.",".$pagesize." ";
			
		}
		
  		$result = $db->fetchAll($sql);
		
  		return $result;
  		
  	}

  	public function listFeed($arrParam = null, $option = null){
  		$db = Zend_Registry::get('connectDB');
  		
  		$pagesize = $arrParam['pagesize'];	
  		$offset = ($arrParam['page'] - 1) * $pagesize;
  		
  		$length = $arrParam['total'] - $offset;
  		
  		if ($length < 0 ){
  			$offset = ($arrParam['page'] - 1) * $length;
  		}
  		
  		$sql = "select * from post inner join feeduser on post.id =feeduser.postid  where feeduser.userfeedid = '".$arrParam['userfeedid']."'order by post.id DESC limit ".$offset.",".$pagesize;
  		
  		$result = $db->fetchAll($sql);
  		
  		return $result;
  	}
  	
  	public function updateFeed($post){
  		
  		if($post["status"] == "new"){
  			
  			$feed = new Feed_Model_Index();
  			$arr = array(
			"id"=>$post["id"],
			"status"=> "old",			
			);
			
			$feed->updateStatusFeed($arr);
  			
  		}
  		
  	}
  	
  	function checkRss($categoryid){
  		
  		$db = Zend_Registry::get('connectDB');
  		
  		$datenow = date('Y-m-d h:i:s',time());
  		
  		$parts = explode('-', $datenow); 
  		
  		$year =  $parts[0];
  		$month = $parts[1];  		
  		$day = $parts[2];

  		
  		
  		$sql = "select * from post where type = 'rss' and categoryid = '".$categoryid."' and YEAR(createdate) = '".$year."' and MONTH(createdate) = '".$month."' and DAY(createdate) = '".$day."' order by id DESC limit 1 ";
  		
  		$result = $db->fetchRow($sql);
  		
  		 		
  		
  		
  		if ($result){
  			
  			return TRUE;
  			
  		}
  		
  		return FALSE;  		 
  		
  	}
  	
  	public function getContent($content,$number){
  		
  		
  		
  		$split = explode(" ",$content);
  		
  		if(count($split) <= $number){
  			
  			return $content;
  		}
  		
  		
  		
  		$result = "";
  		
  		for($i=0;$i<$number;$i++){
  			$result .= $split[$i]." ";

  			
  		}
  		
  		return $result."...";
  		
  	}
  	
  	public function  getLink($url,$val){
  		if($val["type"] == "rss"){
  			return 	$val["link"];	
  		}else{
  			
  			return $url."/post?id=".$val["userid"]."&pid=".$val["id"];
  		}
  	}
  	
  	public function getChanelByPost($idchanel){
  		$chanel = new Chanel_Model_Index();
  		return $chanel->getChanelById($idchanel);
  	}
  	
	public function getUserByPost($iduser){
  		$user = new Registration_Model_Auth();
  		return $user->getItemById($iduser);
  	}
	
  	public function getPostByChanel($arrParam = null){

  		$db = Zend_Registry::get('connectDB');
  		
  		$pagesize = $arrParam['pagesize'];	
  		$offset = ($arrParam['page'] - 1) * $pagesize;
  		
  		
  		$sql = "select DISTINCT  post.userid from post inner join user on post.userid = user.id  where post.chanelid = '".$arrParam['chanelid']."' and post.userid <>'".$arrParam['userid']."'order by post.id DESC limit ".$offset.",".$pagesize;
  		
  		$result = $db->fetchAll($sql);
  		
  		return $result;
  		
  	}
  	
  	public function getPostByChanelUserTop($arrParam = null){
  		
  		$db = Zend_Registry::get('connectDB');
  		$sql = "select * from post inner join user on post.userid = user.id where post.chanelid = '".$arrParam['chanelid']."' and post.userid = '".$arrParam['userid']."'order by post.id DESC limit 1";  		
  		$result = $db->fetchRow($sql);
		
  		return $result;
  		
  	}
  	
}

?>